package com.lovo.dao.impl;

import com.lovo.bean.ChannelBean;
import com.lovo.dao.IChannelDao;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository
public class ChannelDaoImpl extends BaseDao implements IChannelDao {
    @Override
    public List<ChannelBean> findAllChannel() {
        List<ChannelBean> list = new ArrayList<>();
        this.setConnection();
        try {

            ps = con.prepareStatement("SELECT c.*,COUNT(pk_postId) num FROM t_channel c LEFT JOIN t_post m ON c.`pk_channelId`=m.`fk_channelId` GROUP BY c.`pk_channelId`");
            rs = ps.executeQuery();
            while (rs.next()){
                ChannelBean channelBean = new ChannelBean();
                channelBean.setId(rs.getInt("pk_channelId"));
                channelBean.setName(rs.getString("c_name"));
                channelBean.setChannelDescribe(rs.getString("c_depict"));
                channelBean.setPostNum(rs.getInt("num"));
                list.add(channelBean);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            this.closeConnection();
        }
        return list;
    }
}
